package cn.gudian.cms.GuestBook

class GuestBookController {

  def index = { redirect(action: "list", params: params) }

  // the delete, save and update actions only accept POST requests
  static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

  def list = {
    params.max = Math.min(params.max ? params.max.toInteger() : 10, 100)
    [guestBookInstanceList: GuestBook.list(params), guestBookInstanceTotal: GuestBook.count()]
  }

  def create = {
    def guestBookInstance = new GuestBook()
    guestBookInstance.properties = params
    return [guestBookInstance: guestBookInstance]
  }

  def save = {
    def guestBookInstance = new GuestBook(params)
    guestBookInstance.subTime = new Date();
    guestBookInstance.reTime=new Date();
    guestBookInstance.ip = request.getRemoteAddr();
    guestBookInstance.audit = false;
    if (!guestBookInstance.hasErrors() && guestBookInstance.save()) {
      flash.message = "留言成功"
      flash.args = [guestBookInstance.id]
      flash.defaultMessage = "GuestBook ${guestBookInstance.id} created"
      redirect(controller:'c',action: "guest")
    }
    else {
    
    redirect(controller:'c',action: "guest")
    }
  }

  def show = {
    def guestBookInstance = GuestBook.get(params.id)
    if (!guestBookInstance) {
      flash.message = "guestBook.not.found"
      flash.args = [params.id]
      flash.defaultMessage = "GuestBook not found with id ${params.id}"
      redirect(action: "list")
    }
    else {
      return [guestBookInstance: guestBookInstance]
    }
  }

  def edit = {
    def guestBookInstance = GuestBook.get(params.id)
    if (!guestBookInstance) {
      flash.message = "guestBook.not.found"
      flash.args = [params.id]
      flash.defaultMessage = "GuestBook not found with id ${params.id}"
      redirect(action: "list")
    }
    else {
      return [guestBookInstance: guestBookInstance]
    }
  }

  def update = {
    def guestBookInstance = GuestBook.get(params.id)
    guestBookInstance.reTime=new Date();
    if (guestBookInstance) {
      if (params.version) {
        def version = params.version.toLong()
        if (guestBookInstance.version > version) {

          guestBookInstance.errors.rejectValue("version", "guestBook.optimistic.locking.failure", "Another user has updated this GuestBook while you were editing")
          render(view: "edit", model: [guestBookInstance: guestBookInstance])
          return
        }
      }
      guestBookInstance.properties = params
      if (!guestBookInstance.hasErrors() && guestBookInstance.save()) {
        flash.message = "guestBook.updated"
        flash.args = [params.id]
        flash.defaultMessage = "GuestBook ${params.id} updated"
        redirect(action: "show", id: guestBookInstance.id)
      }
      else {
        render(view: "edit", model: [guestBookInstance: guestBookInstance])
      }
    }
    else {
      flash.message = "guestBook.not.found"
      flash.args = [params.id]
      flash.defaultMessage = "GuestBook not found with id ${params.id}"
      redirect(action: "edit", id: params.id)
    }
  }

  def delete = {
    def guestBookInstance = GuestBook.get(params.id)
    if (guestBookInstance) {
      try {
        guestBookInstance.delete()
        flash.message = "guestBook.deleted"
        flash.args = [params.id]
        flash.defaultMessage = "GuestBook ${params.id} deleted"
        redirect(action: "list")
      }
      catch (org.springframework.dao.DataIntegrityViolationException e) {
        flash.message = "guestBook.not.deleted"
        flash.args = [params.id]
        flash.defaultMessage = "GuestBook ${params.id} could not be deleted"
        redirect(action: "show", id: params.id)
      }
    }
    else {
      flash.message = "guestBook.not.found"
      flash.args = [params.id]
      flash.defaultMessage = "GuestBook not found with id ${params.id}"
      redirect(action: "list")
    }
  }

  def deleteAll = {
    def qstr = params.qstr
    if (qstr) {
      try {
        GuestBook.executeUpdate("delete GuestBook where id in(" + qstr + ")")
        flash.message = "delete successfully:" + qstr
        redirect(action: "list")
      } catch (Exception e) {
        flash.message = "delete failed:" + qstr
        redirect(action: "list")
      }
    }
  }
  def audit={
    def qstr = params.qstr
    if (qstr) {
      try {
        GuestBook.executeUpdate("update GuestBook set audit=true where id in(" + qstr + ")")
        flash.message = "审核成功:" + qstr
        redirect(action: "list")
      } catch (Exception e) {
        flash.message = "审核失败:" + qstr
        redirect(action: "list")
      }
    }
  }
}
